const Mock = require('mockjs')
const Random = Mock.Random
const List = []

Random.extend({
  level: function(number) {
    var levels = ['兴趣社团', '院属社团', '校级社团']
    return this.pick(levels)
  },
  org: function() {
    var majors = ['计算机服务队', '软件俱乐部', '自行车协会', '红会', '学生会']
    return this.pick(majors)
  },
  status: function() {
    var statuss = ['活跃', '暂停', '解散']
    return this.pick(statuss)
  }
})

for (let i = 0; i < 100; i++) {
  List.push(Mock.mock({
    id: i,
    name: '@Org',
    position: '@Position',
    level: '@Level',
    nums: '@string(number,2)',
    principal: '@first',
    status: '@Status',
    intro: '据掌上德宏消息，4月8日，中共瑞丽市委召开全市领导干部大会，宣布调整市委主要领导的决定。\n' +
      '\n' +
      '德宏州委常委、州委组织部部长李志明宣布省委、州委干部任职决定：翟玉龙任中共德宏州委委员、常委、瑞丽国家重点开发开放试验区党工委副书记；任中共瑞丽市委委员、常委、书记，兼任中国(云南)自由贸易试验区德宏片区党工委书记。\n' +
      '\n' +
      '翟玉龙在会上表态，我完全拥护省委、州委的决定，坚决服从组织的安排，这是组织对我的信任、重托和考验，我一定带头讲政治，不断提高政治执行力，担负起瑞丽当前工作的重任，迅速转变角色，迅速进入状态，迅速同大家一起实实在在抓紧干事。当前，正是瑞丽疫情防控工作最吃劲的关键阶段，我将与全市领导干部一起深刻汲取教训，切实引以为戒，把疫情防控、边境管控作为当前压倒一切重中之重的工作，切实转变作风，紧紧围绕“问题清单、措施清单、责任清单、时限清单”，狠抓各项工作落实，全力以赴补齐短板、漏洞，坚决尽快打好打赢疫情防控这场硬仗，守好筑牢祖国西南安全屏障。',
    logo: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F-Po3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2Fbd315c6034a85edf1ef4cc334a540923dd5475a1.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1620506668&t=43008dd2f5e6b58ec562397499ed5c71'
  }))
}

module.exports = [
  {
    url: '/org-manage-system/org/list',
    type: 'get',
    response: config => {
      const { type, title, page, limit, onlyWorking } = config.query
      var mockList = List
      const status = onlyWorking
      const listAfterFilter = mockList.filter(
        (item, index) => {
          var flag = true
          let statusFlag = true
          if (title != null && title !== '') {
            flag = false
            if (type != null && title !== '') {
              flag = item[type].search(title) >= 0
            } else {
              flag = item.name.search(title) >= 0 || item.level.search(title) >= 0 || item.principal.search(title) >= 0
            }
          }
          if (status === 'true') {
            statusFlag = item.status.search('活跃') >= 0
          }
          return flag && statusFlag
        }
      )
      const pageList = listAfterFilter.filter(
        (item, index) => {
          return index < limit * page && index >= limit * (page - 1)
        }
      )
      return {
        status: 200,
        data: {
          total: listAfterFilter.length,
          items: pageList
        }
      }
    }
  },
  {
    url: '/org-manage-system/org/id',
    type: 'get',
    response: config => {
      const { id } = config.query
      return {
        status: 200,
        data: {
          items: List[id]
        }
      }
    }
  },
  {
    url: '/org-manage-system/org/names',
    type: 'get',
    response: config => {
      const { page, limit, vague } = config.query
      let names = config.query.names
      if (typeof names === 'string') {
        const temp = names
        names = []
        names.push(temp)
      }
      let mockList = List
      mockList = mockList.filter(
        (item, index) => {
          for (const i in names) {
            if (vague && item.name.search(names[i]) >= 0) {
              return true
            } else if (item.name === names[i]) {
              return true
            }
          }
          return false
        }
      )
      const pageList = mockList.filter(
        (item, index) => {
          return index < limit * page && index >= limit * (page - 1)
        }
      )
      return {
        status: 200,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  }
]
